import {evaluate} from 'mathjs';
import {getObjAttrFun} from "./ObjUtil";
import {isArray, isBoolean, isFunction, isNumber, isObject} from "./TypeUtil";
import {getLocale} from "../../i18n/i18n";
import {getUserInfo} from "../services/SecurityService.tsx";
/**
 * 字符串
 */

/**
 * 字符串模板格式化工具 支持 ("您好,${a}", {a: "admin"}) => 您好，admin
 * @param str 模板字符串
 * @param obj 模板中需要的值会到该对象中找
 * @param matchRegx 匹配的正则表达式 默认/\$\{([^\}]*)\}/g
 * @returns {String}
 */
//eslint-disable-next-line
// @ts-ignore
const formatStr: Function = (str: string, obj: object, matchRegx: any = /\$\{([^\}]*)\}/g) => {
    if (!str || !obj) {
        return str;
    }
    const userInfo = getUserInfo()
    obj = {...obj, userInfo};
    let tmps = str.match(matchRegx);
    if (!tmps) return str;
    tmps.forEach(item => {
        const attrName = item.substring(2, item.length - 1);
        const attrNameArr = attrName.split("||");
        let attrVal;
        attrNameArr.some(k => {
            let val = getObjAttrFun(obj, k);
            if (isFunction(val)) {
                val = val();
            }
            if (val) {
                attrVal = val;
                return true;
            }
        });
        attrVal = attrVal !== undefined ? attrVal : "";
        if (isObject(attrVal) || isArray(attrVal)) {
            str = str.replace(item, JSON.stringify(attrVal));
        } else if (isNumber(attrVal)) {
            const isAll: boolean = str.length === item.length;
            str = isAll ? attrVal : str.replace(isAll ? "\"" + item + "\"" : item, attrVal);
            const operatorRegex = /[\+\-\*\/\%]/;
            if (operatorRegex.test(str)) {
                try {
                    str = evaluate(str);
                } catch (e) {
                }
            }
        } else if (isBoolean(attrVal)) {
            const isAll: boolean = str.length === item.length;
            str = isAll ? attrVal : str.replace(isAll ? "\"" + item + "\"" : item, attrVal);
        } else {
            str = str.replace(item, attrVal);
        }
    });
    return str;
};

// @ts-ignore
const formatLocaleStr: Function = (str: string, obj: object) => {
    if (!str || !obj) {
        return str;
    }
    let arr: string[] = str.split("|");
    if (arr.length > 1) {
        // @ts-ignore
        return getLocale(obj[arr[0]], obj[arr[1]]);
    }
    // @ts-ignore
    return getLocale(obj[arr[0]], obj[arr[0]]);
};

// @ts-ignore
const formatLocalTitle: Function = (str: string, obj: any) => {
    if (!str || !obj) {
        return str;
    }
    let title: any = {
        zh: "",
        en: "",
        show: ""
    };
    let arr: string[] = str.split("-");
    arr.forEach((item: any, index: number) => {
        let itemArr: string[] = item.split("|");
        let localZh = obj[itemArr[0]];
        let localEn = obj[itemArr[1] || itemArr[0]];
        title.zh += localZh + (index === arr.length - 1 ? "" : "-");
        title.en += localEn + (index === arr.length - 1 ? "" : "-");
        title.show += getLocale(localZh, localEn) + (index === arr.length - 1 ? "" : "-")
    });
    return title;
}

const generateRandomId: Function = (totalLength = 20) => {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    const charactersLength = characters.length;
    for (let i = 0; i < totalLength; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

    return result;
}
export {
    formatStr,
    formatLocaleStr,
    formatLocalTitle,
    generateRandomId
};